package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

/*
    hibernate 完成表单校验
 */
@Controller
@RequestMapping("/")
public class UserController {
    @RequestMapping("/jy")
    public String addUser(@Validated({GroupA.class}) User user, Errors errors, Model m) {

        if (errors.hasErrors()) {
            List<FieldError> fieldErrors = errors.getFieldErrors();

            for (FieldError error : fieldErrors) {

                m.addAttribute(error.getField(), error.getDefaultMessage());

            }
            return "";

        }
        return "success.jsp";

    }

}
